package com.example.mydemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mydemo.entity.MenuRole;
import com.example.mydemo.entity.UserRole;
import com.example.mydemo.entity.domain.MenuRoleVO;
import com.example.mydemo.entity.domain.UserRoleVO;
import com.example.mydemo.mapper.MenuRoleMapper;
import com.example.mydemo.mapper.UserRoleMapper;
import com.example.mydemo.service.IMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syf
 * @since 2025-05-28
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {
    @Autowired
    private MenuRoleMapper menuRoleMapper;
    public List<MenuRoleVO> selectByRoleName(String name) {
        return menuRoleMapper.selectByRoleName(name);
    }
    public List<MenuRoleVO> selectByRoleId(Long id) {
        return menuRoleMapper.selectByRoleId(id);
    }
    public List<MenuRoleVO> selectName() {
        return menuRoleMapper.selectName();
    }
    public void delete(Long id){
        MenuRole userRole = new MenuRole();
        userRole.setIsDelete(1L);
        menuRoleMapper.update(
                userRole,
                new UpdateWrapper<MenuRole>().eq("id", id));
    }
    public int deleteByRoleId(Long id){
       return menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("role_id",id));
    };
    public void saveUser(MenuRoleVO userRoleVO){
        MenuRole userRole = new MenuRole();
        userRole.setMenuId(Math.toIntExact(userRoleVO.getMenuId()));
        userRole.setRoleId(Math.toIntExact(userRoleVO.getRoleId()));
        userRole.setCreateBy(userRoleVO.getCreateBy());
        userRole.setCreateTime(userRoleVO.getCreateTime());
        menuRoleMapper.insert(userRole);
    }
}
